package com.custom.custapi.pojo;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Objects;

/**
 * @author zl
 * @version 1.0
 * @description 分页信息
 * @date 2025/1/1 1:1
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SuperBasePager<T> {

    /**
     * 页码
     */
    private Integer num = 0;

    /**
     * 分页大小"
     */
    private Integer size = 10;

    /**
     * data
     */
    private List<T> data;

    private Boolean last;

    private Integer totalNum;

    private Integer totalPage;

    public SuperBasePager(int num, int size) {
        this.num = num;
        if (num <= 1) {
            this.num = 0;
        }
        this.size = size;
    }

    /**
     * 自定义分页
     *
     * @param list 列表
     * @param pageNum 页码
     * @param pageSize 分页大小
     * @return Pager
     */
    @SuppressWarnings("unchecked")
    public static <T> SuperBasePager<T> setTaskPage(List<?> list, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        SuperBasePager<T> pager = new SuperBasePager<>();
        pager.setNum(pageNum);
        pager.setSize(pageSize);
        if (end >= list.size()) {
            end = list.size();
            pager.setLast(true);
        } else {
            pager.setLast(false);
        }
        List<T> sub = (List<T>) CollUtil.sub(list, start, end);
        pager.setData(sub);
        pager.setTotalNum(list.size());
        if (list.size() % pageSize == 0) {
            pager.setTotalPage(list.size() / pageSize);
        } else {
            pager.setTotalPage(list.size() / pageSize + 1);
        }
        return pager;
    }

    public SuperBasePager<T> to(IPage<T> iPage) {
        if (Objects.isNull(iPage)) {
            return null;
        }
        return new SuperBasePager<>((int) iPage.getCurrent(), (int) iPage.getSize(), iPage.getRecords(), iPage.getPages() <= iPage.getCurrent(), (int) iPage.getTotal(), (int) iPage.getPages());
    }
}
